import asyncio
import ygg_aiomysql as aiomysql

async def main():
    """
    主函数: 演示分页查询完整流程
    """
    # 创建数据库连接池
    pool = await aiomysql.new_pool_env()
    
    try:
        # 创建测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("""
                    CREATE TABLE IF NOT EXISTS test_table (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        name VARCHAR(100),
                        age INT
                    )
                """)
                await conn.commit()
        
        # 插入多条测试数据
        test_data = [
            {'name': f'用户{i}', 'age': 20 + i} for i in range(1, 11)
        ]
        print(f"准备插入测试数据: {test_data}")
        
        for data in test_data:
            await aiomysql.add(pool, 'test_table', data)
        print("测试数据插入完成")
        
        # 测试第一页
        print("\n查询第一页数据(每页3条):")
        page1 = await aiomysql.get_page(pool, 'test_table', 1, 3)
        for item in page1:
            print(f"ID: {item['id']}, 姓名: {item['name']}, 年龄: {item['age']}")
        
        # 测试第二页
        print("\n查询第二页数据(每页3条):")
        page2 = await aiomysql.get_page(pool, 'test_table', 2, 3)
        for item in page2:
            print(f"ID: {item['id']}, 姓名: {item['name']}, 年龄: {item['age']}")
        
        # 测试最后一页
        print("\n查询最后一页数据(每页3条):")
        page4 = await aiomysql.get_page(pool, 'test_table', 4, 3)
        for item in page4:
            print(f"ID: {item['id']}, 姓名: {item['name']}, 年龄: {item['age']}")
        
    finally:
        # 清理测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("DROP TABLE IF EXISTS test_table")
                await conn.commit()
        
        # 关闭连接池
        pool.close()
        await pool.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())